package com.fenxiangtech.mall.manager;

import com.fenxiangtech.mall.dao.CartDAO;
import com.fenxiangtech.mall.model.Cart;
import com.fenxiangtech.mall.model.qc.CartQC;
import com.fenxiangtech.mall.model.qc.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class CartManager {

    @Autowired
    private CartDAO cartDAO;

    public List<Cart> queryCart(Cart cart, Integer pageIndex , Integer pageSize){
        Page page = new Page(pageIndex,pageSize);
        CartQC qc  = new CartQC();
        qc.setPage(page);
        BeanUtils.copyProperties(cart,qc);
        List<Cart> carts = cartDAO.listCart(qc);
        return carts;
    }

    public Long countCart(Cart cart){
        CartQC qc  = new CartQC();
        BeanUtils.copyProperties(cart,qc);
        Long count = cartDAO.countCart(qc);
        return count;
    }

    public int saveCart(Cart cart){

        if(cart.getId()!=null){
            return cartDAO.updateCart(cart);
        }else {
            cart.setCreateTime(new Date());
            return cartDAO.insertCart(cart);
        }

    }
    public int deleteCart(Long id){

        return cartDAO.deleteCartById(id);

    }



}
